﻿/* 
 Copyright (C) 2011  Amblox Project

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Text;
using Amblox.Encoding;

namespace Amblox
{
    public class FuseBuilder
    {
        private StringBuilder Builder;

        internal FuseBuilder(string text)
        {
            Builder = new StringBuilder(text);
        }

        internal FuseBuilder(int header)
        {
            Builder = new StringBuilder(JeaxEncoding.encodeB64(header, 2));
        }

        internal FuseBuilder()
        {
            Builder = new StringBuilder();
        }

        internal void Initialize(int header)
        {
            Append(JeaxEncoding.encodeB64(header, 2));
        }

        internal void Initialize(string header)
        {
            Append(header);
        }


        internal void AppendBoolean(bool b)
        {
            if (b)
                Builder.Append(JeaxEncoding.encodeVL64(1));
            else
                Builder.Append(JeaxEncoding.encodeVL64(0));
        }

        internal void AppendBreak()
        {
            AppendChar(2);
        }

        internal void AppendWired(object o)
        {
            try
            {
                Builder.Append(JeaxEncoding.encodeVL64(int.Parse(Convert.ToString(o))));
            }
            catch { }
        }

        internal void AppendB64(object o)
        {
            try
            {
                Builder.Append(JeaxEncoding.encodeB64(int.Parse(Convert.ToString(o)), 2));
            }
            catch { }
        }

        internal void Append(object o)
        {
            Builder.Append(o);
        }

        internal void AppendString(object o)
        {
            Builder.Append(o.ToString() + Convert.ToChar(2));
        }

        internal void AppendString(object o, int breaker)
        {
            Builder.Append(o.ToString() + Convert.ToChar(breaker));
        }

        internal void AppendString(object o, char breaker)
        {
            Builder.Append(o.ToString() + breaker);
        }

        internal void AppendChar(int c)
        {
            Builder.Append(Convert.ToChar(c));
        }

        public override string ToString()
        {
            return Builder.ToString();
        }

        public int Length
        {
            get
            {
                return Builder.Length;
            }
        }

        internal void Clear()
        {
            Builder.Clear();
        }
    }
}